package leetcode.editor.cn;

/**
 * 反转链表的练习
 */
public class FanZhuanLianBiaoLcof{
    public static void main(String[] args) {
        Solution solution = new FanZhuanLianBiaoLcof().new Solution();
        ListNode n1=new ListNode(1);
        ListNode n2=new ListNode(2);
        ListNode n3=new ListNode(3);
        ListNode n4=new ListNode(4);
        ListNode n5=new ListNode(5);
        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        n4.next=n5;
        ListNode listNode = solution.reverseList(n1);
    }

//leetcode submit region begin(Prohibit modification and deletion)

 //Definition for singly-linked list.
public static class ListNode {
 int val;
 ListNode next;
 ListNode(int x) { val = x; }
}
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null) return head;
        ListNode pre = null,cur=head;
        
        while(cur.next!=null){
            ListNode next = cur.next;
            cur.next = pre;
            pre=cur;
            cur=next;
        }
        cur=pre;
        return cur;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}